﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GP.Logic;
using GP.Logic.Model;
using Mvc.Models;

namespace Mvc.Controllers
{
    [Authorize]
    public partial class CitizenController : Controller
    {
        private readonly CitizenService _service = new CitizenService();
        private readonly DoctorService _doctorService = new DoctorService();
        //
        // GET: /Citizen/

        public virtual ActionResult CreateAccess(long citizenId, long docId, string expires)
        {
            DateTime expiresDateTime = expires.GetDate();
            _service.CreateAccess(citizenId, docId, expiresDateTime);
            return RedirectToAction(MVC.Citizen.Access(citizenId));
        }

        public virtual ActionResult DeleteAccess(long citizenId, long docId)
        {
            _service.DeleteAccess(citizenId, docId);
            return RedirectToAction(MVC.Citizen.Access(citizenId));
        }

        public virtual ActionResult Access(long citizenId)
        {
            var model = new CitizenAccessModel();
            model.Citizen = _service.GetCitizen(citizenId);
            model.Doctors = _doctorService.GetDoctorsWhichDontHaveGrantedAccess(citizenId);
            return View(Views.Access, model);
        }

        public virtual ActionResult Index()
        {
            var model = _service.GetCitizens();
            return View(Views.Index, model);
        }

        public virtual ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Citizen/Create

        public virtual ActionResult CreateCitizen()
        {
            var citizen = _service.CreateCitizen();
            return RedirectToAction(Actions.Edit(citizen.CitizenId));
        } 

        public virtual ActionResult Edit(long id)
        {
            var model = _service.GetCitizen(id);
            return View(Views.Edit, model);
        }

        //
        // POST: /Citizen/Edit/5

        [HttpPost]
        public virtual ActionResult Edit(long citizenId, Citizen citizen)
        {
            try
            {
                _service.SaveCitizen(citizenId, citizen);
                return RedirectToAction(MVC.Citizen.Index());
            }
            catch
            {
                return View(citizen);
            }
        }

        //
        // GET: /Citizen/Delete/5

        public virtual ActionResult Delete(long citizenId)
        {
            _service.DeleteCitizen(citizenId);

            return RedirectToAction(Actions.Index());
        }
    }
}
